package com.njsoft.bodyawakening.http;




import com.njsoft.bodyawakening.utils.NetWorkUtils;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * author: Cuiernao
 * date:   On 2018/9/14
 */
public class BaseModel<T> {
    private Disposable disposable;

    protected void setMyCallback(final ModelListener<T> listener, Observable<T> observable) {

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<T>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(T s) {

                      listener.modelSuccessful(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.modelError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void cancel() {
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
